package org.jeecg.modules.database.sharding.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.database.sharding.constats.LogConstant;
import org.jeecg.modules.database.sharding.mapper.ShardingApiLogMapper;
import org.jeecg.modules.database.sharding.service.IShardingApiLogService;
import org.jeecg.modules.database.sharding.entity.ShardingApiLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统日志表 服务实现类
 * </p>
 */
@Service
@DS("sharding")
@Slf4j
public class ShardingApiLogServiceImpl extends ServiceImpl<ShardingApiLogMapper, ShardingApiLog> implements IShardingApiLogService {


    @Resource
    private ShardingApiLogMapper shardingApiLogMapper;

    @Override
    public List<ShardingApiLog> listByDate(String datetime) {
        return shardingApiLogMapper.listByDate(datetime);
    }

    @Override
    public IPage<ShardingApiLog> queryByCondition(JSONObject jsonObject, Page<ShardingApiLog> page) throws Exception{
        String beginDate = jsonObject.getString("beginDate");
        String endDate = jsonObject.getString("endDate");
        Date begin;
        Date end;
        log.info("query: {}", jsonObject);
        if (!"".equals(beginDate) && !"".equals(endDate)) {
           begin = new SimpleDateFormat(LogConstant.Date_FORMAT).parse(beginDate);
           end = new SimpleDateFormat(LogConstant.Date_FORMAT).parse(endDate);
        } else {
            //set begin time to 2022-01-01
            begin = new Date(DateUtil.beginOfMonth(new Date()).getTime());
            end = new Date(DateUtil.endOfMonth(new Date()).getTime());
        }

        String clientId = jsonObject.getString("clientId");
        String apiName = jsonObject.getString("apiName");
        String isServiceLog = jsonObject.getString("isServiceLog");
        String logType = jsonObject.getString("logType");

        LambdaQueryWrapper<ShardingApiLog> result = new LambdaQueryWrapper<>();

        result.eq(ShardingApiLog::getIsServiceLog, isServiceLog);

        if (isServiceLog.equals("1") && ObjectUtil.isNotEmpty(logType)) {
            result.eq(ShardingApiLog::getApiNameEn, logType);
        }

        if (!ObjectUtil.isEmpty(clientId)) {
            result.eq(ShardingApiLog::getClientId, clientId);
        }

        if (!ObjectUtil.isEmpty(apiName)) {
            result.like(ShardingApiLog::getApiName, apiName);
        }
        result.between(ShardingApiLog::getCreateTime, begin, end);

        result.orderByDesc(ShardingApiLog::getCreateTime);

        return page(page, result);
    }
}
